﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Data.ConnectionUI;
using System.Windows.Forms;

namespace System.DbSizeForecaster
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IMainWindowView
    {
        MainWindowPresenter _presenter;

        public object SelectedRow
        {
            get
            {
                return this.dataGridTables.SelectedItem;
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            _presenter = new MainWindowPresenter(this);
        }

        private void buttonOpenProject_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DataSizing project (*.dsproj)|*.dsproj";

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                _presenter.OpenProject(openFileDialog.FileName);
        }

        private void buttonSaveProject_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Filter = "DataSizing project (*.dsproj)|*.dsproj"
            };

            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                _presenter.SaveProject(saveFileDialog.FileName);
        }

        private void buttonSqlDatabase_Click(object sender, RoutedEventArgs e)
        {
            DataConnectionDialog dialog = new DataConnectionDialog();

            DataConnectionConfiguration dcd = new DataConnectionConfiguration(null);

            dcd.LoadConfiguration(dialog);

            if (DataConnectionDialog.Show(dialog) == System.Windows.Forms.DialogResult.OK)
            {
                _presenter.SelectDatabase(dialog.ConnectionString);
            }
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
            _presenter.UpdateTableAndDbSize();
        }

        private void buttonRemove_Click(object sender, RoutedEventArgs e)
        {
            _presenter.RemoveSelectedRow();
        }

        public void ShowTableSizes(ProjectData projectData)
        {
            dataGridTables.ItemsSource = projectData.ProjectDetails;
        }

        public void ShowUserMessage(string msg)
        {
            System.Windows.Forms.MessageBox.Show(msg);
        }

        public double TotalDatabaseSize
        {
            get
            {
                return double.Parse(this.textBoxTotalSize.Text);
            }
            set
            {
                this.textBoxTotalSize.Text = value.ToString("n2");
            }
        }


        public string SelectedConnectionString
        {
            get
            {
                return this.textBoxConnectionString.Text;
            }
            set
            {
                this.textBoxConnectionString.Text = value;
            }
        }

        public void ShowErrorMessage(string msg)
        {
            System.Windows.Forms.MessageBox.Show(msg, System.Windows.Forms.Application.ProductName);
        }

        public double? PercentageReserveForDataAndIndexes
        {
            get
            {
                double val = 0;
                if (double.TryParse(this.textBoxIndexReserve.Text, out val))
                    return val;

                return null;
            }
            set
            {
                this.textBoxIndexReserve.Text = value.ToString();
            }
        }

        public bool UseMSSpaceUsed
        {
            get
            {
                return this.checkBox1.IsChecked.Value;
            }
            set
            {
                this.checkBox1.IsChecked = value;
            }
        }

        private void textBoxIndexReserve_TextChanged(object sender, TextChangedEventArgs e)
        {
            
        }

        private void textBoxIndexReserve_LostFocus(object sender, RoutedEventArgs e)
        {
            _presenter.UpdateAvgEntitySize();
        }

        private void dataGridTables_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
